require 'test/unit'
require 'watir'
require 'watir/contrib/enabled_popup'
require 'features/step_definitions/parser'


Browser = Watir::IE

browser = Browser.new
email_parser = EmailListParser.new

Before do
  $browser = browser
  $email_parser = email_parser
end

at_exit do
  $browser.close
end

def check_for_popups
  autoit = WIN32OLE.new('AutoItX3.Control')
  loop do
    ret = autoit.WinWait('Message from webpage', '', 1)
    if (ret==1) then autoit.ControlClick('Message from webpage', '', '[CLASS:Button;INSTANCE:1]') end
    sleep(1)
  end
end

class AddTag
  def initialize
  end

  def verify_tag(string,add_tags_id,tag_value)
    string = $browser.div(:id, add_tags_id).html
    tag_id = string.scan(/tag\d*\d/)
    i=0
    a = Array.new
    while i<tag_id.size
      tag = $browser.span(:id,tag_id[i]).text
      a[i] = tag
      i+=1
    end
    i=0
    while(a[i] != tag_value)
      if i<a.size
        i+=1
      else
        puts("tag #{tag_value} not found under #{add_tags_id}")
        return
      end
    end
    puts("tag #{tag_value} was found under #{add_tags_id}")
  end

  def delete_tag(string,add_tags_id,tag_value)
    string = $browser.div(:id, add_tags_id).html
    tag_id = string.scan(/tag\d*\d/)
    i=0

    while i<tag_id.size
      if($browser.span(:id,tag_id[i]).text==tag_value)
        $browser.span(:id,tag_id[i]).link(:class,"tag-unlink bullet-delete").click
        return()
      else
        i+=1
      end
    end
  end

  def add_tag(string,add_tags_id,tag_value)
    add_tag_id = Array.new
    add_tag_id = string.scan(/obj-tag-add-link-\d*\d/)
    $browser.link(:id, add_tag_id[0]).click
    string = $browser.div(:id, add_tags_id).html
    tag_input = string.scan(/tag-input-\d*\d/)
    $browser.text_field(:id, tag_input[0]).set(tag_value)
    $browser.div(:id, add_tags_id).link(:class, "lnk_tag_btn").click
  end


  def tag_placement(method,email_id,tag_value)
    msg_id = email_id.delete!("msg")
    email_id = "minf" + msg_id

    $browser.div(:id, "msg#{msg_id}").div(:class,"mr-text-1").click
    sleep(2)

    string = $browser.div(:id, email_id).html
    add_tags_id = Array.new
    add_tags_id = string.scan(/obj-tags-\d*\d/)
    string = $browser.div(:id, add_tags_id[0]).html

    case method
    when "add_tag" then add_tag(string,add_tags_id[0].to_s(),tag_value)
    when "verify_tag" then verify_tag(string,add_tags_id[0].to_s(),tag_value)
    when "delete_tag" then delete_tag(string,add_tags_id[0].to_s(),tag_value)
    else raise("wrong method specifies = #{method}")
    end

  end

  #     execute_method { method , tag_value}
  #
  #     Walk through email list on the page and add/verify/delete specifyed tags
  #     depends on method called for each email found.
  #
  #     execute_method(add_tag,tag_value)
  #         Add tag_value to each email found on the page
  #     execute_method(verify_tag,tag_value) - not implemented yet
  #         Verifies if each email that was found on the page containe tag with
  #         tag_value
  #     execute_method(delete_tag,tag_value) - not implemented yet
  #         Delete tags with tag_value in each email that was found on the page
  def execute_method(method,tag_value)
    sleep(3)
    string = $browser.div(:id, 'message_list_container').html
    match = Array.new
    match = string.scan(/msg\d*\d/)
    i=0
    while (i<match.size)
      #      puts(method)
      tag_placement(method,match[i],tag_value)
      i+=1
    end
  end
end